package com.hoow.shakoow.utils.analytics.timezone

import java.util.*

object TimeZoneUtil {

    /**
     * 获取当前地区
     *
     */
    fun currentTimeZoneCountry(): String {
        var country = TimeZoneHostEnum.TimeZoneNaira.country
        val timeZoneId = TimeZone.getDefault().id.toString()
        println("======timeZoneId=$timeZoneId")
        when (timeZoneId) {
            TimeZoneHostEnum.TimeZoneGha.timeZoneId -> {
                country = TimeZoneHostEnum.TimeZoneGha.country
            }
            TimeZoneHostEnum.TimeZoneFcfa.timeZoneId -> {
                country = TimeZoneHostEnum.TimeZoneFcfa.country
            }
            TimeZoneHostEnum.TimeZoneKsh.timeZoneId -> {
                country = TimeZoneHostEnum.TimeZoneKsh.country
            }
            TimeZoneHostEnum.TimeZoneUgx.timeZoneId -> {
                country = TimeZoneHostEnum.TimeZoneUgx.country
            }
            TimeZoneHostEnum.TimeZoneNaira.timeZoneId -> {
                country = TimeZoneHostEnum.TimeZoneNaira.country
            }
            TimeZoneHostEnum.TimeZoneVietnam.timeZoneId -> {
                country = TimeZoneHostEnum.TimeZoneVietnam.country
            }
        }
        return country
    }

    /**
     * 获取当前需要回传数据的地区
     */
    private fun currentTimeZoneSupportCountrys(): List<String> {
        val list = mutableListOf<String>()
        val timeZoneId = TimeZone.getDefault().id.toString()
        println("======timeZoneId=$timeZoneId")
        when (timeZoneId) {
            TimeZoneHostEnum.TimeZoneGha.timeZoneId -> {
                list.add(TimeZoneHostEnum.TimeZoneGha.country)
            }
            TimeZoneHostEnum.TimeZoneFcfa.timeZoneId -> {
                list.add(TimeZoneHostEnum.TimeZoneFcfa.country)
            }
            TimeZoneHostEnum.TimeZoneKsh.timeZoneId -> {
                list.add(TimeZoneHostEnum.TimeZoneKsh.country)
            }
            TimeZoneHostEnum.TimeZoneUgx.timeZoneId -> {
                list.add(TimeZoneHostEnum.TimeZoneUgx.country)
            }
            TimeZoneHostEnum.TimeZoneNaira.timeZoneId -> {
                list.add(TimeZoneHostEnum.TimeZoneNaira.country)
            }
            TimeZoneHostEnum.TimeZoneVietnam.timeZoneId -> {
                list.add(TimeZoneHostEnum.TimeZoneVietnam.country)
            }
        }
        return list
    }

    /**
     *  转换为真正需要上报数据的服务器地址列表
     *
     * @param list 配置的服务器地址
     */
    fun covertToRealTargetServers(list: List<String>): List<String> {
        val curTimeZoneSupportCountrys: List<String> = currentTimeZoneSupportCountrys()
        val serviceTargetUrlList = list.filter { url ->
            curTimeZoneSupportCountrys.any { country -> url.endsWith(country) }
        }
        val resultTargetUrlList = serviceTargetUrlList.ifEmpty { list }
        println("==== resultTargetUrlList:$resultTargetUrlList")
        return resultTargetUrlList
    }

}